#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const LL mod = 1e9 + 7;
map<LL, int>mp;
LL n, k;

bool dfs(LL u,LL cnt)
{
	if (!u && !cnt) {
		return 1;
	}
	if (u < 0)return 0;
	//cout << "u==" << u << endl;
	for (int i = log10(n) / log10(3); i >= 0; i--) {
		LL x = pow(3, i);
		dfs(u - x, cnt - 1);
	}
}
int main()
{
	//cout << log10(1e18) / log10(3) << endl;
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> k;
		//cout << log10(n) / log10(3) << endl;
		if (n == k) {
			cout << "Yes" << endl;
			continue;
		}
		LL sum = 0;
		bool flag = 0;
		int cnt = 0;
		//k = max(k, n - k);
		for (int i = log10(n) / log10(3); i >= 0; i--) {
			LL x = pow(3, i);
			sum += x;
			cnt++;
			if (n - sum == k - cnt || cnt + x == k) {
				flag = 1;
				break;
			}
		}
		if (flag)cout << "Yes" << endl;
		else cout << "No" << endl;
		/*if (dfs(n,k))cout << "Yes" << endl;
		else cout << "No" << endl;*/
	}
	return 0;
}